package repository

import (
	"fmt"
	"wechat-push/internal/model"
	"wechat-push/pkg/http"
	"wechat-push/pkg/logger"
)

const (
	messageUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s"
	tokenUrl   = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s"
)

func SendWechatMessage(token string, datas model.WechatMessageDatas) (resp model.WechatMessageResp, err error) {
	reqUrl := fmt.Sprintf(messageUrl, token)
	err = http.Post(reqUrl, datas, &resp)
	if err != nil {
		logger.Log.Error("http.Post err : ", err)
		return
	}

	return
}

func GetWechatToken(appId, secret string) (resp model.WechatTokenResp, err error) {
	reqUrl := fmt.Sprintf(tokenUrl, appId, secret)
	err = http.Get(reqUrl, &resp)
	if err != nil {
		logger.Log.Error("http.Get err : ", err)
		return
	}
	return
}
